using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using MULENetworking;

namespace MULEServer
{
    public class NetworthCalculator
    {
        public NetworthCalculator(IPlayer player1, IPlayer player2, IPlayer player3, IPlayer player4, Store store)
        {
            player1.landworth = calculateLand(player1, store);
            player1.goodsworth = calculateGoods(player1, store);
            player1.networth = calculateNetworth(player1, store);

            player2.landworth = calculateLand(player2, store);
            player2.goodsworth = calculateGoods(player2, store);
            player2.networth = calculateNetworth(player2, store);

            player3.landworth = calculateLand(player3, store);
            player3.goodsworth = calculateGoods(player3, store);
            player3.networth = calculateNetworth(player3, store);

            player4.landworth = calculateLand(player4, store);
            player4.goodsworth = calculateGoods(player4, store);
            player4.networth = calculateNetworth(player4, store);

            ArrayList playerOrder = new ArrayList();
            ArrayList playersToOrder = new ArrayList();

            playerOrder.Add(player1);
            playersToOrder.Add(player2);
            playersToOrder.Add(player3);
            playersToOrder.Add(player4);

            Boolean playerInserted;
            IPlayer tempPlayer, playerToCompare;
            for (int x = 0; x < 3; x++)
            {
                playerInserted = false;
                tempPlayer = (IPlayer)playersToOrder[x];
                for (int y = 0; y < playerOrder.Count; y++)
                {
                    playerToCompare = (IPlayer)playerOrder[y];
                    if(tempPlayer.networth > playerToCompare.networth)
                    {
                        playerOrder.Insert(y, tempPlayer);
                        playerInserted = true;
                        break;
                    }
                }
                if (playerInserted == false)
                {
                    playerOrder.Add(tempPlayer);
                }
            }

            for (int i = 0; i < playerOrder.Count; i++)
            {
                tempPlayer = (IPlayer)playerOrder[i];
                tempPlayer.place = i + 1;
            }

            

        }

        public int calculateMoney(IPlayer player, Store store)
        {
            return player.money;
        }

        public int calculateGoods(IPlayer player, Store store)
        {
            return (player.food * (store.FoodPrice + 35)
                        + player.energy * (store.EnergyPrice + 35)
                        + player.smithore * (store.SmithorePrice + 50)
                        + player.crystite * (store.CrystitePrice + 140));
        }

        public int calculateLand(IPlayer player, Store store)
        {
            int total = 0;
            for(int x = 0; x < player.plotsOwned.Count; x++)
            {
                IMapSquare currentSquare = (IMapSquare)player.plotsOwned[x];
                switch (currentSquare.Mule)
                {
                    case "empty":
                        total += 500;
                        break;
                    case "food":
                        total += 525;
                        break;
                    case "energy":
                        total += 550;
                        break;
                    case "smithore":
                        total += 575;
                        break;
                    case "crystite":
                        total += 600;
                        break;
                }
            }
            return total;
        }

        public int calculateNetworth(IPlayer player, Store store)
        {
            int total = 0;

            total += calculateMoney(player, store);
            total += calculateGoods(player, store);
            total += calculateLand(player, store);
            return total;
        }
    }
}
